var that ;
class Tab{
    constructor(id){
        //获取元素
        that = this;
        this.main =document.querySelector(id);
        this.add = document.querySelector('.tabadd');
        this.ul =this.main.querySelector('.fisrstnav ul:first-child');
        this.fsection = document.querySelector('.tabscon');
        this.init();
    }
    //获取所有的li和section
    updateNode(){
        this.lis = document.querySelectorAll('li');
        this.sections = document.querySelectorAll('section');
        this.remove = document.querySelectorAll('.icon-guanbi');
        this.spans = document.querySelectorAll('.fisrstnav li span:first-child')
        this.section = document.querySelectorAll('.tabscon section:first-child')
    }

    init(){
        this.updateNode();
        // init初始化操作让相关的元素绑定事件 
        this.add.onclick = this.addTab;
        for(var i = 0;i<this.lis.length;i++){
            this.lis[i].index = i;
            this.lis[i].onclick = this.toggleTab;
            this.remove[i].onclick=this.removeTaB;
            this.spans[i].ondblclick = this.editTab;
            this.sections[i].ondblclick = this.editTab;
          
        }
       
    }
    //切换功能
    toggleTab(){
        that.clearClass();
        this.className = 'liactive';
        that.sections[this.index].className = 'conactive';
    }
    
    clearClass(){
        for(var i =0 ;i<this.lis.length;i++){
            this.lis[i].className = "";
            this.sections[i].className = "";

        }
    }
    //添加功能
    addTab(){
        that.clearClass();
        //1.创建li元素和section元素
        var random = Math.random();
        var li = '<li class="liactive"><span>新增选项卡</span><span class="iconfont icon-guanbi"></span></li>';
        var section = '<section class="conactive">新增内容'+random+'</section>';
        //2.把这两个元素追加到对应的父元素里面
        that.ul.insertAdjacentHTML('beforeend',li);
        that.fsection.insertAdjacentHTML('beforeend',section);
        that.init();

    }
    //删除功能
    removeTaB(e){
        e.stopPropagation();
        var index = this.parentNode.index;
        console.log(index);
        that.lis[index].remove();
        that.sections[index].remove();
        that.init();
        //当我们删除的不是选中状态打li ，让其他li保持不变
        if(document.querySelector('.liactive')) return;
        //当我们删除了选中状态打li ，让前一个li处于选定状态
        index--;
        //手动调用点击事件，不需要鼠标触发
        that.lis[index] && that.lis[index].click();
    }
    //修改功能
    editTab(){
        var str = this.innerHTML;
        // 双击禁止选中文字
        window.getSelection ? window.getSelection().removeAllRanges() : document.selec;
        // alert(11);
        this.innerHTML = '<input type="text" />';
        var input = this.children[0];
        input.value = str;
        input.select();//让文本框里面的文字处于选中状态
        //当离开文本框把里面的值给span
        input.onblur = function(){
            this.parentNode.innerHTML = this.value;
        }
        //按下回车也可以把文本框里面的span
        input.onkeyup = function(e){
            if(e.keyCode ===13){
                this.blur();
            }
        }
    }
}
new Tab("#tab");